class GUI:
    def __init__(self):
        from tkinter import Tk
        self.mainWindows=Tk()
        self.mainWindows.geometry("420x250")
        self.mainWindows.title("Base64转换")
        self.CreatButton()
        self.CreatText()
        self.ShowText()
        self.mainWindows.mainloop()
    def CreatButton(self):
        from tkinter import Button
        self.Encode_btn=Button(self.mainWindows,text="Base64编码",command=lambda : self.GetFromText(self.OriginText)).place(relx=0.4,rely=0.25,relwidth=0.2)
        self.Decode_btn=Button(self.mainWindows,text="Base64解码",command=lambda : self.GetFromText(self.Base64Text)).place(relx=0.4,rely=0.55,relwidth=0.2)
    def CreatText(self):
        from tkinter import Text
        self.OriginText=Text(self.mainWindows)
        # self.OriginText.bind("<Control-Key-a>", self.SelectText)
        self.OriginText.bind("<Control-Key-a>", lambda event:self.OriginText.tag_add('sel', '0.0', 'end'))
        self.Base64Text=Text(self.mainWindows)
        self.Base64Text.bind("<Control-Key-a>", self.SelectText)
    def SelectText(self, event):
        self.Base64Text.tag_add('sel', '0.0', 'end')
    def ShowText(self):
        self.OriginText.place(rely=0.1,relheight=0.8,relwidth=0.4)
        self.Base64Text.place(relx=0.6,rely=0.1,relheight=0.8,relwidth=0.4)
    def GetFromText(self,InputText):
        if(InputText==self.OriginText):
            base64code=Base64Code(InputText.get("0.0","end")[:-1])
            self.Base64Text.delete(0.0,"end")
            self.Base64Text.insert(0.0,base64code.encode())
        else:
            base64code=Base64Code(self.Base64Text.get("0.0","end")[:-1])
            self.OriginText.delete(0.0,"end")
            self.OriginText.insert(0.0,base64code.decode())
class Base64Code:
    def __init__(self,sinput):
        self.sInput=sinput
    def encode(self):
        from base64 import b64encode
        return b64encode(self.sInput.encode()).decode()
    def decode(self):
        from base64 import b64decode
        return b64decode(self.sInput.encode()).decode()
    def test_output(self):
        print("self.sInput: "+self.sInput)
gui=GUI()